<?php

/**
 * This is the model class for table "property".
 *
 * The followings are the available columns in table 'property':
 * @property integer $id
 * @property string $name
 * @property string $type
 * @property string $category
 * @property string $description
 * @property string $url
 * @property string $country
 * @property string $city
 * @property string $area
 * @property string $street_address
 * @property double $lat
 * @property double $lng
 * @property integer $price
 * @property string $creat_at
 * @property string $last_update
 * @property integer $number_of_rooms
 * @property integer $number_of_bedrooms
 * @property integer $number_of_bathrooms
 * @property integer $floor
 * @property integer $total_floor
 * @property integer $square
 * @property integer $phone
 * @property integer $status
 * @property string $main_photo
 * @property string $photo_1
 * @property string $photo_2
 * @property string $photo_3
 * @property string $photo_4
 * @property string $video
 * @property integer $feature
 * @property integer $agent_id
 * @property string $meta_description
 * @property string $meta_keywords
 * @property string $reference
 *
 * The followings are the available model relations:
 * @property Agent $agent
 * @property PropertyAdditional[] $propertyAdditionals
 */
class Property extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'property';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('price, number_of_rooms, number_of_bedrooms, number_of_bathrooms, floor, total_floor, square, phone, status, feature, agent_id', 'numerical', 'integerOnly'=>true),
			array('lat, lng', 'numerical'),
			array('name, meta_description', 'length', 'max'=>255),
			array('type, category, country, city, area, main_photo, photo_1, photo_2, photo_3, photo_4, video, reference', 'length', 'max'=>45),
			array('description, url, street_address, creat_at, last_update, meta_keywords', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, type, category, description, url, country, city, area, street_address, lat, lng, price, creat_at, last_update, number_of_rooms, number_of_bedrooms, number_of_bathrooms, floor, total_floor, square, phone, status, main_photo, photo_1, photo_2, photo_3, photo_4, video, feature, agent_id, meta_description, meta_keywords, reference', 'safe', 'on'=>'search'),
			array('main_photo', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'), 
			array('photo_1', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'), 
			array('photo_2', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'), 
			array('photo_3', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'), 
			array('photo_4', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'), 

		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'agent' => array(self::BELONGS_TO, 'Agent', 'agent_id'),
			'propertyAdditionals' => array(self::HAS_MANY, 'PropertyAdditional', 'property_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'type' => 'Type',
			'category' => 'Category',
			'description' => 'Description',
			'url' => 'Url',
			'country' => 'Country',
			'city' => 'City',
			'area' => 'Area',
			'street_address' => 'Street Address',
			'lat' => 'Lat',
			'lng' => 'Lng',
			'price' => 'Price',
			'creat_at' => 'Creat At',
			'last_update' => 'Last Update',
			'number_of_rooms' => 'Number Of Rooms',
			'number_of_bedrooms' => 'Number Of Bedrooms',
			'number_of_bathrooms' => 'Number Of Bathrooms',
			'floor' => 'Floor',
			'total_floor' => 'Total Floor',
			'square' => 'Square',
			'phone' => 'Phone',
			'status' => 'Status',
			'main_photo' => 'Main Photo',
			'photo_1' => 'Photo 1',
			'photo_2' => 'Photo 2',
			'photo_3' => 'Photo 3',
			'photo_4' => 'Photo 4',
			'video' => 'Video',
			'feature' => 'Feature',
			'agent_id' => 'Agent',
			'meta_description' => 'Meta Description',
			'meta_keywords' => 'Meta Keywords',
			'reference' => 'Reference',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('category',$this->category,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('country',$this->country,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('street_address',$this->street_address,true);
		$criteria->compare('lat',$this->lat);
		$criteria->compare('lng',$this->lng);
		$criteria->compare('price',$this->price);
		$criteria->compare('creat_at',$this->creat_at,true);
		$criteria->compare('last_update',$this->last_update,true);
		$criteria->compare('number_of_rooms',$this->number_of_rooms);
		$criteria->compare('number_of_bedrooms',$this->number_of_bedrooms);
		$criteria->compare('number_of_bathrooms',$this->number_of_bathrooms);
		$criteria->compare('floor',$this->floor);
		$criteria->compare('total_floor',$this->total_floor);
		$criteria->compare('square',$this->square);
		$criteria->compare('phone',$this->phone);
		$criteria->compare('status',$this->status);
		$criteria->compare('main_photo',$this->main_photo,true);
		$criteria->compare('photo_1',$this->photo_1,true);
		$criteria->compare('photo_2',$this->photo_2,true);
		$criteria->compare('photo_3',$this->photo_3,true);
		$criteria->compare('photo_4',$this->photo_4,true);
		$criteria->compare('video',$this->video,true);
		$criteria->compare('feature',$this->feature);
		$criteria->compare('agent_id',$this->agent_id);
		$criteria->compare('meta_description',$this->meta_description,true);
		$criteria->compare('meta_keywords',$this->meta_keywords,true);
		$criteria->compare('reference',$this->reference,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Property the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
